package com.ctlele.team.space.activity.impl;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.ctlele.team.space.R;
import com.ctlele.team.space.activity.BaseActivity;
import com.ctlele.team.space.adapter.AppPagerAdapter;
import com.ctlele.team.space.entity.TabEntity;
import com.ctlele.team.space.fragment.impl.HomeFragment;
import com.ctlele.team.space.fragment.impl.MyFragment;
import com.ctlele.team.space.widget.view.FixedViewPager;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends BaseActivity {

    private static final String TAG = "HomeActivity";

    /**
     * 底部tab项标题
     */
    private static final List<String> sTitles = new ArrayList<String>() {
        {
            add("首页");
            add("我的");
        }
    };

    /**
     * 未选择状态的icon
     */
    private static final int[] sIconUnselectIds = {
            R.mipmap.home_unselect,
            R.mipmap.my_unselect
    };

    /**
     * 选择状态的icon
     */
    private static final int[] sIconSelectIds = {
            R.mipmap.home_selected,
            R.mipmap.my_selected
    };

    /**
     * 控制view的切换
     */
    private FixedViewPager mViewPager;

    /**
     * 控制tab的切换
     */
    private CommonTabLayout mCommonTabLayout;

    /**
     * view集合
     */
    private List<Fragment> mFragmentList = new ArrayList<>();

    /**
     * tab集合
     */
    private List<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected int initLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        mViewPager = findViewById(R.id.viewpager);
        mCommonTabLayout = findViewById(R.id.common_tab_layout);
    }

    @Override
    protected void initData() {
        mFragmentList.add(HomeFragment.newInstance());
        mFragmentList.add(MyFragment.newInstance());

        // 装载tab
        for (int i = 0; i < sTitles.size(); i++) {
            mTabEntities.add(new TabEntity(sTitles.get(i), sIconSelectIds[i], sIconUnselectIds[i]));
        }

        // tab集合交给tab控制器管理
        mCommonTabLayout.setTabData((ArrayList<CustomTabEntity>) mTabEntities);
        mCommonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        // view集合交给view控制器管理
        // adapter控制view的切换
        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setAdapter(new AppPagerAdapter(getSupportFragmentManager(), mFragmentList, sTitles));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                // 当view切换时 改变对应tab
                mCommonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
}